需要 Scripting PRO
BluetoothCentralManager 提供了用于操作 BLE 中央设备的核心接口,包括扫描附近蓝牙设备、连接外设、获取已知设备、断开连接等能力。适用于实现如外设控制、数据采集、IoT通信等典型蓝牙场景。
isScanning: Promise<boolean>获取当前是否正在扫描外围设备。
类型:Promise<boolean>
示例:
startScan(onDiscoverPeripheral, options?): Promise<void>启动 BLE 设备扫描,直到调用
stopScan()结束。每发现一个设备都会触发onDiscoverPeripheral回调。
onDiscoverPeripheral: (peripheral, advertisementData, rssi) => void
每发现一个外围设备时调用
参数说明:
peripheral: BluetoothPeripheral 外设对象advertisementData: BluetoothAdvertisementData 广播数据rssi: number 信号强度(dBm)options?: { services?: string[]; allowDuplicates?: boolean; solicitedServiceUUIDs?: string[] }
services: 只扫描包含指定服务 UUID 的外设allowDuplicates: 是否允许重复回调同一设备,默认 falsesolicitedServiceUUIDs: 一个包含外设请求的服务 UUID 的数组,表明希望由中央设备提供哪些服务在使用 BluetoothCentralManager.startScan() 进行蓝牙扫描时,每次发现设备都会返回包含该设备广播数据的 advertisementData 对象。该对象包含设备在广播包中附带的多种信息字段,用于识别、过滤、分类外设。
| 字段名 | 类型 | 说明 |
|---|---|---|
localName |
string(可选) |
外设广播的本地名称(若有)。用于展示用户可识别的设备名称。 |
txPowerLevel |
number(可选) |
发射功率(单位 dBm)。用于估算设备距离,RSSI + TxPower 可用于计算距离。 |
manufacturerData |
Data(可选) |
厂商自定义数据,常用于识别设备型号、序列号等。需自行解析 Data。 |
serviceData |
Record<string, Data>(可选) |
服务数据字段,键为服务 UUID,值为对应的服务内容(Data 类型)。 |
serviceUUIDs |
string[](可选) |
广播中声明支持的服务 UUID 列表。可用于快速判断设备功能类型。 |
overflowServiceUUIDs |
string[](可选) |
当 serviceUUIDs 超出广播数据包大小限制时,会将溢出部分放入该字段。 |
isConnectable |
boolean(可选) |
该设备是否支持连接。扫描结果中用于快速过滤无法连接的广播型设备。 |
solicitedServiceUUIDs |
string[](可选) |
外设请求的服务 UUID,表明希望由中央设备提供哪些服务。 |
localName 或 serviceUUIDs 进行设备筛选manufacturerData 判断厂商/设备类型txPowerLevel 和 RSSI 估算设备距离isConnectable 判断是否需要尝试连接manufacturerData 和 serviceData 是原始二进制数据(Data 类型),需根据厂商协议解析serviceUUIDs 仅代表广播包中声明的服务,完整服务需通过 discoverServices() 获取Promise<void>:扫描启动成功时 resolve,失败时 rejectstopScan(): Promise<void>停止正在进行的扫描操作。
Promise<void>:成功停止时 resolveretrievePeripherals(ids: string[]): Promise<BluetoothPeripheral[]>根据设备 UUID 获取已知的蓝牙设备(可能已连接或已配对)。
ids: string[]:设备的唯一标识符数组Promise<BluetoothPeripheral[]>:返回符合 ID 的设备列表retrieveConnectedPeripherals(serviceUUIDs: string[]): Promise<BluetoothPeripheral[]>获取当前连接中并提供指定服务的外围设备。
serviceUUIDs: string[]:过滤条件,仅返回包含这些服务 UUID 的设备Promise<BluetoothPeripheral[]>:匹配的设备列表connect(peripheral, options?): Promise<void>与指定外围设备建立连接。
peripheral: BluetoothPeripheral:要连接的设备
options?: 可选连接配置:
startDelay?: number:延迟连接(秒)enableTransportBridging?: boolean:启用传输桥接(用于特殊外设)requiresANCS?: boolean:是否需要 ANCS 支持(苹果通知服务)enableAutoReconnect?: boolean:是否自动重连notifyOnConnection?: boolean - 是否通知app连接成功notifyOnDisconnection?: boolean - 是否通知app连接已断开notifyOnNotification?: boolean - 是否通知app收到通知Promise<void>:连接成功 resolve,失败 rejectdisconnect(peripheral): Promise<void>断开与指定外围设备的连接。此操作是非阻塞的,部分尚未完成的操作可能无法继续。
peripheral: BluetoothPeripheral:要断开的设备Promise<void>onDisconnected 回调将被调用discoverServices() 发现服务,然后通过 discoverCharacteristics() 获取特征值后再读写。stopScan(),避免后台持续运行。